package com.foreknow.saxbuilder;

import com.foreknow.dao.UserDao;
import com.foreknow.model.User;
import com.foreknow.service.UserServie;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassPathXMLApplicationContext implements BeanFactory {
    //创建一个Map容器
    private Map<String,Object> beans=new HashMap<String, Object>();
    //在构造中要解析beans.xml文件
    public ClassPathXMLApplicationContext() throws JDOMException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //创建jdm中提供好的SAXBuilder对象
        SAXBuilder saxBuilder=new SAXBuilder();
        //读取beans.xml文件
        Document doc=saxBuilder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml"));
        //获取到根元素
        Element root = doc.getRootElement();
        //获取到子元素<bean>
        List list = root.getChildren();
        for (int i=0;i<list.size();i++){
            Element element = (Element)list.get(i);
            //获取到bean中的属性值
            String id = element.getAttributeValue("id");
            String aClass = element.getAttributeValue("class");
            //我们需要将clazz转换为Object对象
            Object o = Class.forName(aClass).newInstance();
           //将获取到的id保存到Map容器中

            beans.put(id,o);
            //获取bean中的property
            for (Element propertyElement:(List<Element>)element.getChildren("property")
                 ) {
                String name=propertyElement.getAttributeValue("name");//userDAo
                String bean=propertyElement.getAttributeValue("bean");//u

                //获取map容器中的UserDAOImpl
                Object beanObject = beans.get(bean);//UserDAOImpl对象
                //调用public void setUserDAO(UserDAO userDAO)方法
                String methodName="set"+name.substring(0,1).toUpperCase()+name.substring(1);
                Method method = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
                //调用set方法 o:UserService对象 beanObject就是UserDAO接口的实现类(实际参数)
                method.invoke(o,beanObject);
            }
        }
    }
    public Object getBean(String id) {

        return beans.get(id);
    }

    public static void main(String[] args) throws JDOMException, IOException, IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, SQLException {
//        BeanFactory beanFactory=new ClassPathXMLApplicationContext();
//        UserServie servie=new UserServie();
//        UserDao userDao=(UserDao)beanFactory.getBean("u");
//        servie.setUserDao(userDao);
//        servie.add(new User());
        BeanFactory factory=new ClassPathXMLApplicationContext();
        UserServie userService =(UserServie) factory.getBean("userService");
        userService.add(new User());
    }
}
